package com.example.android.xxm;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    /*private ViewPager viewPager;
    private ImageView home, category, car, people;
    private LinearLayout ll1, ll2, ll3, ll4;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;*/

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private LayoutInflater mInflater;
    private List<String> mTitleList = new ArrayList<>();//页卡标题集合
    private View view1, view2, view3, view4;//页卡视图
    private List<View> mViewList = new ArrayList<>();//页卡视图集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*mInflater = LayoutInflater.from(this);
        view1 = mInflater.inflate(R.layout.hello, null);
        view2 = mInflater.inflate(R.layout.hello, null);
        view3 = mInflater.inflate(R.layout.hello, null);
        view4 = mInflater.inflate(R.layout.hello, null);
        //添加页卡视图
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        mViewList.add(view4);

        //添加页卡标题
        mTitleList.add("No:1");
        mTitleList.add("No:2");
        mTitleList.add("No:3");
        mTitleList.add("No:4");

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));//添加tab选项卡
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(2)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(3)));

        MyPagerAdapter mAdapter = new MyPagerAdapter(mViewList);
        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器*/

        /*exit.getInstance().addActivity(this);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        home = (ImageView) findViewById(R.id.home);
        category = (ImageView) findViewById(R.id.message);
        car = (ImageView) findViewById(R.id.car);
        people = (ImageView) findViewById(R.id.people);

        ll1 = (LinearLayout) findViewById(R.id.ll1);
        ll2 = (LinearLayout) findViewById(R.id.ll2);
        ll3 = (LinearLayout) findViewById(R.id.ll3);
        ll4 = (LinearLayout) findViewById(R.id.ll4);


       mFragments = new ArrayList<Fragment>();
        Fragment F1 = new fragment1();
        Fragment F2 = new CategoryFragment();
        Fragment F3 = new fragment3();
        Fragment F4 = new fragment4();
        mFragments.add(F1);
        mFragments.add(F2);
        mFragments.add(F3);
        mFragments.add(F4);


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        viewPager.setAdapter(mAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                int currentItem = viewPager.getCurrentItem();
                setItem(currentItem);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        initEvent();
        //   viewPager.setCurrentItem(0);
        setSelect(0);*/
    }
    //ViewPager适配器
    class MyPagerAdapter extends PagerAdapter {
        private List<View> mViewList;

        public MyPagerAdapter(List<View> mViewList) {
            this.mViewList = mViewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();//页卡数
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));//添加页卡
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));//删除页卡
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);//页卡标题
        }
    }

    /*private void initEvent() {
        ll1.setOnClickListener(this);
        ll2.setOnClickListener(this);
        ll3.setOnClickListener(this);
        ll4.setOnClickListener(this);
    }

    private void setItem(int i) {
        switch (i) {
            case 0:
                home.setImageResource(R.mipmap.home1);
                break;
            case 1:
                category.setImageResource(R.mipmap.message1);
                break;
            case 2:
                car.setImageResource(R.mipmap.car1);
                break;
            case 3:
                people.setImageResource(R.mipmap.people1);
                break;
        }
    }

    private void setSelect(int i) {
        setItem(i);
        viewPager.setCurrentItem(i);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll1:
                setSelect(0);
                break;
            case R.id.ll2:
                setSelect(1);
                break;
            case R.id.ll3:
                setSelect(2);
                break;
            case R.id.ll4:
                setSelect(3);
                break;
        }
    }*/
}
